package jlts.front.ui;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

/**
 * @ClassName MenuFrame
 * @Description TODO
 * @Author Zxeao
 * @Date 2021/12/21 上午11:31
 * @Version V1.0
 */
public class MenuFrame extends JFrame {
    private JLabel titleLabel = new JLabel();
    private JLabel welcomeLabel = new JLabel();
    private JLabel copyrightLabel = new JLabel();

    private JButton examBtn = new JButton();
    private JButton scoreBtn = new JButton();
    private JButton ruleBtn = new JButton();
    private JButton exitBtn = new JButton();

    private JPanel contentPanel = new JPanel();
    private JPanel northPanel = new JPanel();
    private JPanel centerPanel = new JPanel();
    private JPanel southPanel = new JPanel();
    private JPanel welcomePanel = new JPanel();
    private JPanel btnPanel = new JPanel();


    public MenuFrame() {
        this.setSize(550, 450);
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.setLocationRelativeTo(null);
        initContentPanel();
        this.add(contentPanel);
        this.setResizable(false);
        this.setVisible(false);
    }

    private void initContentPanel() {
        //设置面板布局
        contentPanel.setLayout(new BorderLayout());
        //设置面板边框
        contentPanel.setBorder(new EmptyBorder(8, 8, 8, 8));
        //初始化组件
        initNorthPanel();
        initCenterPanel();
        initSouthPanel();
        //添加组件
        contentPanel.add(northPanel,BorderLayout.NORTH);
        contentPanel.add(centerPanel,BorderLayout.CENTER);
        contentPanel.add(southPanel,BorderLayout.SOUTH);
    }

    private void initNorthPanel() {
        northPanel.setLayout(new BorderLayout());
        northPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
        //获取某包内的静态资源，类似于new File("").toURL();但是比其更好用
        URL url=getClass().getResource("img/title.png");
        ImageIcon icon=new ImageIcon(url);
        titleLabel.setIcon(icon);
        titleLabel.setHorizontalAlignment(JLabel.CENTER);
        northPanel.add(titleLabel,BorderLayout.CENTER);
    }

    private void initCenterPanel() {
        centerPanel.setLayout(new BorderLayout());
        centerPanel.setBorder(new EmptyBorder(8, 8, 8, 8));
        initWelcomePanel();
        initBtnPanel();
        centerPanel.add(welcomePanel,BorderLayout.NORTH);
        centerPanel.add(btnPanel,BorderLayout.CENTER);
    }

    private void initWelcomePanel() {
        welcomePanel.setLayout(new FlowLayout());
        welcomePanel.setBorder(new EmptyBorder(8, 8, 8, 8));
        welcomeLabel.setText("欢迎 张三丰  祝考试顺利");
        //后续要更新数据
        welcomeLabel.setHorizontalAlignment(JLabel.CENTER);
        welcomePanel.add(welcomeLabel);

    }

    private void initBtnPanel() {
        ImageIcon examIcon=new ImageIcon(getClass().getResource("img/exam.png"));
        ImageIcon scoreIcon=new ImageIcon(getClass().getResource("img/result.png"));
        ImageIcon ruleIcon=new ImageIcon(getClass().getResource("img/message.png"));
        ImageIcon exitIcon=new ImageIcon(getClass().getResource("img/exit.png"));
        examBtn.setText("开始");
        scoreBtn.setText("分数");
        ruleBtn.setText("规则");
        exitBtn.setText("离开");

        examBtn.setIcon(examIcon);
        scoreBtn.setIcon(scoreIcon);
        ruleBtn.setIcon(ruleIcon);
        exitBtn.setIcon(exitIcon);
        //垂直底端对齐 水平居中对齐
        exitBtn.setVerticalTextPosition(JButton.BOTTOM);
        exitBtn.setHorizontalTextPosition(JButton.CENTER);
        ruleBtn.setVerticalTextPosition(JButton.BOTTOM);
        ruleBtn.setHorizontalTextPosition(JButton.CENTER);
        scoreBtn.setVerticalTextPosition(JButton.BOTTOM);
        scoreBtn.setHorizontalTextPosition(JButton.CENTER);
        examBtn.setVerticalTextPosition(JButton.BOTTOM);
        examBtn.setHorizontalTextPosition(JButton.CENTER);

        btnPanel.add(examBtn);
        btnPanel.add(scoreBtn);
        btnPanel.add(ruleBtn);
        btnPanel.add(exitBtn);

        examBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.print("此功能暂未开发");
            }
        });
        scoreBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("点了分数");
            }
        });
        ruleBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("点了规则");
            }
        });
        exitBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int result=JOptionPane.showConfirmDialog(null, "确定离开了吗？");
                if(result==JOptionPane.YES_OPTION){
                    System.out.println("点了离开");
                }
            }
        });
    }

    private void initSouthPanel() {
        southPanel.setLayout(new FlowLayout());
        southPanel.setBorder(new EmptyBorder(6, 6, 6, 6));
        copyrightLabel.setText("版权所有，翻录必究，达内教育");
        copyrightLabel.setHorizontalAlignment(JLabel.RIGHT);
        southPanel.add(copyrightLabel);
    }

    /**更新错误信息*/
    public void updateWelcome(String realname){
        this.welcomeLabel.setText("欢迎"+realname+",祝考试顺利！");
    }
}
